package com.ky.home.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ky.common.vo.R;
import com.ky.home.entity.VO.WrongQuestionsVO;
import com.ky.home.mapper.WrongQuestionsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import cn.hutool.core.convert.NumberWithFormat;
import com.ky.common.config.SystemConfig;
import com.ky.common.util.JwtUtil;
import com.ky.home.entity.Words;
import com.ky.home.entity.WrongQuestions;
import com.ky.home.mapper.WordsMapper;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author pl
 * @since 2024-06-22
 */
@RestController
@RequestMapping("/home")
public class WrongQuestionsController {
    @Autowired
    private WrongQuestionsMapper wrongquestonMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @RequestMapping("/getErrorQuestions")
    public R getErrorQuestions() {
        Integer errorQuestionID = (Integer) redisTemplate.opsForValue().get("questionid_error");
        List<WrongQuestionsVO> wrongQuestionsVOS = wrongquestonMapper.selectListByExam(errorQuestionID);
        if (wrongQuestionsVOS.size() > 0){
            return R.ok(wrongQuestionsVOS);}
        return R.fail("查询失败");
    }
    @Autowired
    WrongQuestionsMapper wrongQuestionsMapper;
    @Autowired
    WordsMapper wordsMapper;
    @RequestMapping("/wrong-questions/collectionWord/{id}")
    public R collectionWord(HttpServletRequest request,@PathVariable int id){
        String token = request.getHeader(SystemConfig.HEADER_TOKEN);
        Integer userId = ((NumberWithFormat) JwtUtil.parseToken(token, "id")).intValue();
        WrongQuestions wrongQuestions = new WrongQuestions();
        wrongQuestions.setUserId(userId);
        wrongQuestions.setWordId(id);
        wrongQuestions.setType(1);
        wrongQuestions.setAttemptTime(new Date());
        wrongQuestionsMapper.insert(wrongQuestions);
        return R.ok();
    }
    @RequestMapping("/wrong-questions/collectionWordList")
    public R collectionWordList(HttpServletRequest request){
        String token = request.getHeader(SystemConfig.HEADER_TOKEN);
        Integer userId = ((NumberWithFormat) JwtUtil.parseToken(token, "id")).intValue();
        QueryWrapper<WrongQuestions> wrongQuestionsQueryWrapper = new QueryWrapper<>();
        wrongQuestionsQueryWrapper.eq("user_id",userId).eq("type",1);
        List<WrongQuestions> wrongQuestions = wrongQuestionsMapper.selectList(wrongQuestionsQueryWrapper);
        //遍历wrongQuestions
        List<Words> words = new ArrayList<>();
        for (WrongQuestions WrongQuestions : wrongQuestions) {
            QueryWrapper<Words> wordsQueryWrapper = new QueryWrapper<>();
            wordsQueryWrapper.eq("id",WrongQuestions.getWordId());
            words.add(wordsMapper.selectOne(wordsQueryWrapper));
        }
        return R.ok(words);
           /* String token = request.getHeader(SystemConfig.HEADER_TOKEN);
            Integer userId = ((NumberWithFormat) JwtUtil.parseToken(token, "id")).intValue();
            QueryWrapper<WrongQuestions> wrongQuestionsQueryWrapper = new QueryWrapper<>();
            wrongQuestionsQueryWrapper.eq("user_id", userId).eq("type", 1);
            List<WrongQuestions> wrongQuestions = wrongQuestionsMapper.selectList(wrongQuestionsQueryWrapper);
            //遍历wrongQuestions
            List<Words> words = new ArrayList<>();
            for (WrongQuestions WrongQuestions : wrongQuestions) {
                QueryWrapper<Words> wordsQueryWrapper = new QueryWrapper<>();
                wordsQueryWrapper.eq("id", WrongQuestions.getWordId());
                words.add(wordsMapper.selectOne(wordsQueryWrapper));
            }
            return R.ok(words);*/
    }
}

